package com.et.util.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class PageTag extends SimpleTagSupport {
	private int curPage;
	private int count;
	private int pageCount;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out=getJspContext().getOut();
		out.write("<div class=\"newLoopPage\">");
		out.write("\r\n");
		if(curPage > 1){
			out.write("<a href=\"javascript:goPage(1,"+pageCount+");\">首页</a>");
			out.write("\r\n");
			out.write("<a href=\"javascript:goPage("+(curPage-1)+","+pageCount+");\">上一页</a>");
		}
		out.write("\r\n");
		if(pageCount<=4){
			//全部显示
			printNum(out,1,pageCount);
		}else{
			if(curPage == 1){
				printNum(out,1,curPage+1);
			}else if(curPage+1 <= pageCount){
				printNum(out,curPage-1,curPage+1);
			}else if(curPage == pageCount){
				printNum(out,curPage-1,pageCount);
			}
			out.write("\r\n");
		}
		out.write("\r\n");
		if(curPage != pageCount){
			out.write("<a href=\"javascript:goPage("+(curPage+1)+","+pageCount+");\">下一页</a>");
			out.write("\r\n");
			out.write("<a href=\"javascript:goPage("+pageCount+","+pageCount+");\">尾页</a>");
		}
		out.write("\r\n");
		out.write("<p style=\"margin-top:2px\">共计<span style=\"color:red\">"+pageCount+"</span>页</p>");
		out.write("<p>跳到<input type=\"text\" onkeydown=\"return event.keyCode==8 || event.keyCode>=48 && event.keyCode<=57 || event.keyCode>=96&&event.keyCode<=105\" maxlength=\"3\" value=\""+curPage+"\" name=\"curPage\" id=\"curPage\">页  </p>");
		out.write("\r\n");
		out.write("<input type=\"button\" class=\"Redbtn\" value=\"确定\" onclick='javascript:goPage(0,"+pageCount+");' name=\"\" />");
		out.write("</div>");
		out.write("\r\n");
		out.write("");
	}


	private void printNum(JspWriter out,int beginNum,int endNum) throws IOException {
		for(int i=beginNum;i<=endNum;i++){
			if(i==curPage){
				out.write("<a href=\"javascript:goPage("+i+","+pageCount+");\" class=\"selected\">"+i+"</a>");
			}else{
				out.write("<a href=\"javascript:goPage("+i+","+pageCount+");\">"+i+"</a>");
			}
			out.write("\r\n");
		}
	}


	/**
	 * @return the curPage
	 */
	public int getCurPage() {
		return curPage;
	}


	/**
	 * @param curPage the curPage to set
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}


	/**
	 * @return the count
	 */
	public int getCount() {
		return count;
	}


	/**
	 * @param count the count to set
	 */
	public void setCount(int count) {
		this.count = count;
	}


	/**
	 * @return the pageCount
	 */
	public int getPageCount() {
		return pageCount;
	}


	/**
	 * @param pageCount the pageCount to set
	 */
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

}
